import 'package:bboy_music_box_2023/bean/bean_musci_item.dart';
import 'package:bboy_music_box_2023/bean/bean_music_manager.dart';
import 'package:bboy_music_box_2023/bean/bean_music_query.dart';
import 'package:bboy_music_box_2023/controllers/ctrl_play.dart';
import 'package:bboy_music_box_2023/controllers/ctrl_query.dart';
import 'package:bboy_music_box_2023/res/colors.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SelectListView extends StatefulWidget {
  SelectListView({Key? key}) : super(key: key);

  @override
  State<SelectListView> createState() => _SelectListViewState();
}

class _SelectListViewState extends State<SelectListView> {
  late RefreshController controller;

  @override
  void initState() {
    super.initState();
    controller = RefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned.fill(child: buildMusicBody()),
      ],
    );
  }

  Widget buildMusicBody() {
    return Container(
      child: Obx(() {
        var waitMusics = PlayCtrl.to.musicWaitArr.value;
        bool isPlaying = PlayCtrl.to.isPlaying.value;
        String? selectMusicKey = PlayCtrl.to.selectMusic.value.music?.key;

        List<MusicItem> items = [];
        for (var i = 0; i < waitMusics.length; i++) {
          var music = waitMusics[i];
          items.add(MusicItem(1, "", music));
        }

        return SmartRefresher(
          controller: controller,
          enablePullDown: true,
          header: ClassicHeader(
            refreshingText: '正在刷新',
            completeText: '刷新完成',
            failedText: '刷新失败',
            idleText: '下拉刷新',
            releaseText: '松开即可刷新',
          ),
          onRefresh: _onRefresh,
          child: ListView.builder(
            padding: const EdgeInsets.only(bottom: 200.0),
            physics: const BouncingScrollPhysics(),
            itemBuilder: (context, index) {
              MusicItem item = items[index];
              if (item.flag == 1) {
                var music = item.music;

                Widget? playIcon;

                bool isSelectedAndPlaying =
                    music.key == selectMusicKey && isPlaying;

                if (isSelectedAndPlaying) {
                  playIcon = Container(
                    key: ValueKey("playing"),
                    width: 20.0,
                    child: Lottie.asset(
                      "assets/lottie/lottie_playing.json",
                      fit: BoxFit.fill,
                    ),
                  );
                } else {
                  playIcon = Container(
                    key: ValueKey("none"),
                    width: 20,
                    child: Container(
                      alignment: Alignment.center,
                      margin: const EdgeInsets.symmetric(horizontal: 3.0),
                      height: 5.0,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10.0),
                        color: Colors.black,
                      ),
                    ),
                  );
                }

                return InkWell(
                  onTap: () {
                    PlayCtrl.to.playOrPause(item.music);
                  },
                  child: Container(
                    margin: const EdgeInsets.all(5.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        SizedBox(width: 10.0),
                        AnimatedSwitcher(
                          duration: Duration(milliseconds: 500),
                          child: playIcon,
                          transitionBuilder:
                              (Widget child, Animation<double> animation) {
                            return ScaleTransition(
                              scale: animation,
                              child: child,
                            );
                          },
                        ),
                        SizedBox(width: 20.0),
                        Container(
                          width: 40.0,
                          height: 40.0,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5.0),
                          ),
                          child: Image.network(music.imgUrl),
                        ),
                        SizedBox(width: 20.0),
                        Expanded(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                child: Text(
                                  music.name,
                                  maxLines: 1,
                                  softWrap: false,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: isSelectedAndPlaying
                                        ? Colors.green
                                        : Colors.black,
                                    fontSize: 14.0,
                                    fontWeight: FontWeight.normal,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          width: 40.0,
                          height: 40.0,
                          child: Icon(
                            Icons.more_vert,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              }
            },
            itemCount: items.length,
          ),
        );
      }),
    );
  }

  //下拉刷新加载数据
  void _onRefresh() async {
    print('下拉刷新');
    await QueryCtrl.to.queryMusics();
    print('完成刷新');
    controller.refreshCompleted();
  }
}
